package com.aeric.qrpro;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.aeric.CWNetwork.BaseActivity;
import com.aeric.CWUtility.SharedPreferencesUtils;
import com.aeric.domain.UserInfo;

public class UserCenterActivity extends BaseActivity implements View.OnClickListener{
    private Button btn_sign_out;
    private TextView tv_user_id, tv_user_name;
    UserInfo userInfo;
    @Override
    protected int initLayout() {
        return R.layout.activity_user_center;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setupActionBar();
        userInfo = (UserInfo)getIntent().getSerializableExtra("userInfo");
        initView();
        setListener();
        initData();
    }
    /**
     * Set up the {@link android.app.ActionBar}, if the API is available.
     */
    private void setupActionBar() {
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Show the Up button in the action bar.
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayShowCustomEnabled(true);
            actionBar.setTitle(R.string.user_center);
        }
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
//                Intent intent = new Intent();
//                intent.putExtra("userInfo",userInfo);
//                startActivityForResult(intent,RESULT_OK);
                finish();
                break;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void initView() {
        tv_user_id = findViewById(R.id.tv_user_id);
        tv_user_name = findViewById(R.id.tv_user_name);
        btn_sign_out = findViewById(R.id.btn_sign_out);
    }
    private void  initData(){
        tv_user_id.setText(userInfo.userId);
        tv_user_name.setText(userInfo.userName);
    }

    private void setListener() {
        btn_sign_out.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        switch (v.getId()) {
            case R.id.btn_sign_out:
                SharedPreferencesUtils sp = new SharedPreferencesUtils(this,"User");
                sp.deleteObject("UserInfo");
                Intent intent = new Intent();
                intent.setClass(UserCenterActivity.this,LoginActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }
}
